package com.aura.blog.controller;


import com.aura.blog.common.Result;
import com.aura.blog.dto.request.AddBlogRequest;
import com.aura.blog.dto.request.UpdateBlogRequest;
import com.aura.blog.dto.response.BlogInfoResponse;
import com.aura.blog.service.BlogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
@Tag(name = "博客管理", description = "博客相关接口")
public class BlogController {
    @Resource(name = "blogServiceImpl")
    private BlogService blogService;

    /**
     * 获取博客列表
     *
     * @return 博客信息响应列表
     */
    @GetMapping("/getList")
    @Operation(summary = "获取博客列表", description = "获取所有未删除的博客列表，按更新时间倒序排列")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取成功"),
        @ApiResponse(responseCode = "401", description = "未认证")
    })
    public List<BlogInfoResponse> getList() {
        return blogService.getList();
    }

    /**
     * 获取博客详情
     *
     * @param blogId 博客ID，不能为空
     * @return 博客信息响应对象
     */
    @GetMapping("/getBlogDetail")
    @Operation(summary = "获取博客详情", description = "根据博客ID获取博客详细信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误"),
        @ApiResponse(responseCode = "401", description = "未认证"),
        @ApiResponse(responseCode = "404", description = "博客不存在")
    })
    public BlogInfoResponse getBlogDetail(@Parameter(description = "博客ID") @NotNull(message = "blogId 不能为null") Integer blogId) {
        log.info("查询博客详情, blogId:{}", blogId);
        return blogService.getBlogDetail(blogId);
    }

    /**
     * 添加博客
     *
     * @param addBlogRequest 添加博客请求对象，包含博客标题、内容等信息
     * @return 操作结果
     */
    @PostMapping("/add")
    @Operation(summary = "添加博客", description = "发布新的博客文章")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "发布成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误"),
        @ApiResponse(responseCode = "401", description = "未认证")
    })
    public Result addBlog(@Validated @RequestBody AddBlogRequest addBlogRequest) {
        log.info("发布博客, request:{}", addBlogRequest);
        return Result.ok(blogService.addBlog(addBlogRequest));
    }

    /**
     * 更新博客
     *
     * @param updateBlogRequest 更新博客请求对象，包含要更新的博客信息
     * @return 是否更新成功
     */
    @PostMapping("/update")
    @Operation(summary = "更新博客", description = "更新已存在的博客文章")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "更新成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误"),
        @ApiResponse(responseCode = "401", description = "未认证"),
        @ApiResponse(responseCode = "404", description = "博客不存在")
    })
    public Boolean updateBlog(@Validated @RequestBody UpdateBlogRequest updateBlogRequest) {
        log.info("修改博客, request:{}", updateBlogRequest);
        return blogService.updateBlog(updateBlogRequest);
    }

    /**
     * 删除博客（逻辑删除）
     *
     * @param blogId 要删除的博客ID
     * @return 是否删除成功
     */
    @PostMapping("/delete")
    @Operation(summary = "删除博客", description = "逻辑删除指定的博客文章")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "删除成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误"),
        @ApiResponse(responseCode = "401", description = "未认证"),
        @ApiResponse(responseCode = "404", description = "博客不存在")
    })
    public Boolean deleteBlog(@Parameter(description = "博客ID") @NotNull Integer blogId) {
        log.info("删除博客, blogId:{}", blogId);
        return blogService.deleteBlog(blogId);
    }
    
    /**
     * 根据用户ID获取博客列表
     *
     * @param userId 用户ID，不能为空
     * @return 博客信息响应列表
     */
    @GetMapping("/getBlogsByUserId")
    @Operation(summary = "根据用户ID获取博客列表", description = "根据用户ID获取该用户发布的所有博客列表")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误"),
        @ApiResponse(responseCode = "401", description = "未认证")
    })
    public List<BlogInfoResponse> getBlogsByUserId(@Parameter(description = "用户ID") @NotNull Integer userId) {
        log.info("根据用户ID获取博客列表, userId:{}", userId);
        return blogService.getBlogsByUserId(userId);
    }
    
    /**
     * 搜索博客
     *
     * @param keyword 搜索关键字，不能为空
     * @return 博客信息响应列表
     */
    @GetMapping("/search")
    @Operation(summary = "搜索博客", description = "根据标题关键字搜索博客")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "搜索成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误"),
        @ApiResponse(responseCode = "401", description = "未认证")
    })
    public List<BlogInfoResponse> searchBlogs(@Parameter(description = "搜索关键字") @RequestParam String keyword) {
        log.info("搜索博客, keyword:{}", keyword);
        return blogService.searchBlogs(keyword);
    }
}